<?php

namespace app\api\model\mysql;

use app\common\model\TimeModel;

class MallUserAddress extends TimeModel
{
    protected $deleteTime = 'delete_time';
    /**
     * 查询默认为1的地址
     * @param string $openid
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserAddressByDefault(string $openid=''){
        return $this->field('id,realname,mobile,lng,lat,locatarea,address,isdefault,cityarea,areacode')
            ->where("openid",$openid)
            ->where("isdefault",1)
            ->find();
    }

    /**
     * 根据id查询信息
     * @param string $id
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserAddressById(string $id=''){
        return $this->field('id,realname,mobile,cityarea,lng,lat,locatarea,address,isdefault')
            ->where("id",$id)
            ->find();
    }

    /**
     * @param string $openid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserAddressByList(string $openid = ''): array
    {
        return $this->field('id,realname,mobile,lng,lat,locatarea,address,isdefault,cityarea,areacode')
            ->where("openid",$openid)
            ->select()
            ->toArray();
    }
}